package com.yuan.key_code;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.Path;

public class WriteFile {
    public static void main(String[] args) {
        try {
            Configuration conf = new Configuration();
            conf.set("fs.defaultFS","hdfs://localhost:9000");
            conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
            FileSystem fs = FileSystem.get(conf);

            //!缓冲区临时存储将要写入的内容
            byte[] buff = "Hello world".getBytes(); // 要写入的内容

            String filename = "test"; //要写入的文件名 默认创建hdfs的/user/{用户名}/test
            FSDataOutputStream os = fs.create(new Path(filename));

            //!将缓冲区中的内容写入文件
            os.write(buff,0,buff.length);
            System.out.println("Create:"+ filename);
            os.close();
            fs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}